package com.vnaloha.common.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vnaloha.common.model.CartItem;
import com.vnaloha.common.service.CustomerService;
import com.vnaloha.common.service.OrdersService;
import com.vnaloha.common.service.ProductService;

@Controller
@RequestMapping("shopping")
public class ShoppingCartController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private OrdersService oderService;
	
	@RequestMapping(value = "/cart", method = RequestMethod.GET)
	public String cart() {
		return "user/cart";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/cart-add", method = RequestMethod.POST)
	public String addToCart(@RequestParam(value="id", required=false) Integer id,
			@RequestParam(value="quality", required=false) Integer quality,
			HttpSession session) {
		CartItem cartItem = new CartItem(productService.get(id),quality);
		
		if(session.getAttribute("cart") == null)
		{
			List<CartItem> cart = new ArrayList<CartItem>();
			cart.add(cartItem);
			session.setAttribute("cart", cart);
		}else
		{
			boolean isExisted = false;
			List<CartItem> cart =(List<CartItem>) session.getAttribute("cart");
			for(int i = 0; i < cart.size(); i++)
			{
				if(cart.get(i).getId() == id){
					isExisted = true;
					cart.get(i).setQuatity(cart.get(i).getQuatity() + quality);
				}
			}
			if(!isExisted)
			{
				cart.add(cartItem);
			}
			session.setAttribute("cart", cart);
		}
		Float total = 0.0f;
		for(CartItem cartTemp : (List<CartItem>) session.getAttribute("cart")){
			total += (cartTemp.getPrice() * cartTemp.getQuatity());
		}
		session.setAttribute("total", CartItem.getTotal((List<CartItem>) session.getAttribute("cart")));
		return "user/cart";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/cart-update", method = RequestMethod.POST)
	public String updateCart(@RequestParam(value="ids", required=false) int[] ids,
			@RequestParam(value="quatities", required=false) int[] quatities,
			HttpSession session) {
			
				if(session.getAttribute("cart") != null)
				{
					List<CartItem> cart =(List<CartItem>) session.getAttribute("cart");
					for(int i = 0; i < cart.size(); i++){
						cart.get(i).setQuatity(quatities[i]);
					}
					if(ids != null){
						for(int id : ids){
							for(int i = 0; i < cart.size(); i++){
								if(id == cart.get(i).getId()){
									cart.remove(i);
									i--;
								}
							}
						}
					}
					session.setAttribute("cart", cart);
					session.setAttribute("total", CartItem.getTotal(cart));
				}		
		return "user/cart";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/checkout",method = RequestMethod.GET)
	public String checkOut(HttpSession session) {
		
		session.setAttribute("customer", customerService.get(1));
		session.setAttribute("cart", session.getAttribute("cart"));
		session.setAttribute("total", CartItem.getTotal((List<CartItem>) session.getAttribute("cart")));
		return "user/checkout";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/payment-success",method = RequestMethod.POST)
	public String payment(HttpSession session,@RequestParam(value="note", required=false) String note) {
		
		oderService.save((List<CartItem>) session.getAttribute("cart"),
				1, note,  CartItem.getTotal((List<CartItem>) session.getAttribute("cart")));
		return "user/paymentSuccess";
	}
	
}
